<?php namespace Common\Model;
use Hdphp\Model\Model;
//商品管理模型
class Goods extends Model{
	//指定表名
	protected $table = "goods";
	//自动验证
	protected $validate = array(
		//array(字段名,验证方法,错误信息,验证条件,验证时间)
		array('gcategory_cate_id','required','分类不能为空',3,3),
		array('goods_name','required','商品名称不能为空',3,3),
		array('market_price','required','市场价',3,3),
		array('shop_price','required','商城价',3,3),
		array('list_pic','required','列表图不能为空',3,3),
		array('goods_img','required','商品图册不能为空',3,3)
	);
	//自动完成
	protected $auto = array(
		//3是必须处理的表单， 1 是在插入的时候处理
		//管理员ID
		array('admin_admin_id','admin_admin_id','method',3,3),
		//商品货号
		array('goods_sn','goods_sn','method',3,3),
		//上架时间
		array('add_time','time','function',3,3)
	);
	
    //管理员ID
    public function admin_admin_id(){
		return $_SESSION['admin_id'];
    }
	//商品货号
	public function goods_sn(){
		$goods_sn = substr(date("ymdHis"),2,8).mt_rand(100000,999999);
		return $goods_sn;
	}
	
	//商品相册
	public function goods_img(){
		$arrData = Q('post.goods_img');
		// implode 将数组变成字符串
		$goods_img =  implode('|',$arrData);
		return $goods_img;
	}

	//添加商品
	public function store(){
		//一、触发的自动校验
		//1、验证goods
		if(!$this->create()) return false;
		//2、验证goods_detail
		$gdetailModel = new \Common\Model\Goods_detail;
		if(!$gdetailModel->create()){
			//把Goods_detail模型的错误，压入当前模型
			$this->error = $gdetailModel->getError();
			return false;
		}
		//二、添加
		//1、goods
		$goods_id = $this->add();
		//2、goods_detail,向商品详细模型的 data属性 压入2个字段的信息。
		$gdetailModel->data['goods_goods_id'] = $goods_id;
		$gdetailModel->data['goods_img'] = $this->goods_img();
		//执行添加
		$gdetailModel->add();
		//3、goods_attr
		$gattrModel = new \Common\Model\Goods_attr;
		$arr = Q('post.arr');
		//遍历数组，把数组里的每个单元 赋值给 goods_attr里的字段
		if(is_array($arr)){
			foreach($arr as $k=>$v){
				$data = array(
					'ga_value' => $v,
					'fattr_fa_id' =>$k,
					'goods_goods_id' =>$goods_id
				);
				//有值的时候存入库；没值报错，不存入库。
				if(!empty($data['ga_value'])){
					if(!$gattrModel->add($data)){
						$this->error = $gattrModel->getError();
						return false;
					}
				}
			}
		}
		$spec = Q('post.spec');
		if(is_array($spec)){
			foreach($spec as $k=>$v){
					foreach($v['value'] as $kk=>$vv){{
						$data = array(
							'ga_value' => $vv,
							'attr_price'=> $v['attr_price'][$kk],
							'fattr_fa_id' => $k,
							'goods_goods_id' => $goods_id
						);
						//p($data);exit;
						if(!empty($data['ga_value'])){
							if(!$gattrModel->add($data)){
								$this->error = $gattrModel->getError();
								return false;
							}
						}
					}
				}
		    }
		}
		return true;	
	}
	//编辑商品
	public function edit(){
		//一、触发的自动校验
		//1、验证goods
		if(!$this->create()) return false;
		//2、验证goods_detail
		$gdetailModel = new \Common\Model\Goods_detail;
		if(!$gdetailModel->create()){
			//把Goods_detail模型的错误，压入当前模型
			$this->error = $gdetailModel->getError();
			return false;
		}
		//二、修改
		$goods_id = Q('get.goods_id',0,'intval');
		//1、goods
		$this->where("goods_id={$goods_id}")->save();
		//2、goods_detail，向商品详细模型的 data属性 压入2个字段的信息。
		$gdetailModel->data['goods_goods_id'] = $goods_id;
		$gdetailModel->data['goods_img'] = $this->goods_img();
		//执行编辑
		$gdetailModel->where("goods_goods_id={$goods_id}")->save();
		//3、goods_attr
		$gattrModel = new \Common\Model\Goods_attr;
		//(1).删除商品属性表原来的数据
		$gattrModel->where("goods_goods_id=$goods_id")->delete();
		//(2).重新压入 新数据
		$arr = Q('post.arr');
		if(is_array($arr)){
			foreach($arr as $k=>$v){
				$data = array(
					'ga_value' => $v,
					'fattr_fa_id' =>$k,
					'goods_goods_id' =>$goods_id
				);
				if(!empty($data['ga_value'])){
					if(!$gattrModel->add($data)){
						$this->error = $gattrModel->getError();
						return false;
					}
				}
			}
		}
		$spec = Q('post.spec');
		if(is_array($spec)){
			foreach($spec as $k=>$v){
					foreach($v['value'] as $kk=>$vv){{
						$data = array(
							'ga_value' => $vv,
							'attr_price'=> $v['attr_price'][$kk],
							'fattr_fa_id' => $k,
							'goods_goods_id' => $goods_id
						);
						if(!empty($data['ga_value'])){
							if(!$gattrModel->add($data)){
								$this->error = $gattrModel->getError();
								return false;
							}
						}
					}
				}
		    }
		}
		return true;
	}
	

}
 ?>